package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.config.BaseContext;
import com.itheima.po.ShoppingCart;
import com.itheima.po.R;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 地址管理
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart) {
//        BaseContext.getCurrentId()获取当前登录用户的ID
//        需要处理首次添加或再次添加
//        首次添加是创建 再次添加是原有数量+1
//        步骤：
//        1 根据菜品ID或套餐ID查询数据库购物车表中是否有该数据
//        1.1 构建查询条件：user_id 菜品或套餐ID
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lambdaQueryWrapper.eq(shoppingCart.getDishId()!= null , ShoppingCart::getDishId, shoppingCart.getDishId());
        lambdaQueryWrapper.eq(shoppingCart.getSetmealId()!= null , ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCartInDb = shoppingCartService.getOne(lambdaQueryWrapper);
        //        2 如果有该数据则update，如果没没有该数据则save
        if (null == shoppingCartInDb){
//            注意 购物车中没有这个菜品或套餐时，一定注意保存的是前端传过来的shoppingCart
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else {
//            注意 购物车中有数据，一定要注意操作的是shoppingCartInDb
            shoppingCartInDb.setNumber(shoppingCartInDb.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartInDb);
        }

        return R.success(shoppingCart);
    }

    /**
     * 查询默认地址
     * Request URL: http://localhost:8080/shoppingCart/clean
     * Request Method: DELETE
     */
    @DeleteMapping("/clean")
    public R<ShoppingCart> clean() {
//        步骤：
//        1. 构建当前登录用户的查询条件
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( ShoppingCart::getUserId, BaseContext.getCurrentId());
//        2. service调用review方法
        shoppingCartService.remove(queryWrapper);
        return R.success(null);

    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("shoppingCart:{}", shoppingCart);

        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != shoppingCart.getUserId(), ShoppingCart::getUserId, shoppingCart.getUserId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(shoppingCartService.list(queryWrapper));
    }
}
